package org.xiaobai.ai.controller;

import com.google.errorprone.annotations.Var;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.xiaobai.ai.constants.ChatConfigConstant;
import org.xiaobai.ai.service.ChatConfigService;
import org.xiaobai.common.utils.UserUtil;
import org.xiaobai.core.exception.TipException;
import org.xiaobai.core.utils.DateTimeUtil;
import org.xiaobai.core.utils.RedisUtil;
import org.xiaobai.core.utils.WebUtil;
import org.xiaobai.tool.response.Result;
import org.xiaobai.tool.service.ApiService;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName ChatController
 * @Description chat接口
 * @Author dingfeng.xiao
 * @Date 2023/6/30 9:42
 * @Version 1.0
 */
@Api(value = "chat接口", tags = {"chat接口"})
@CrossOrigin
@RestController
@RequestMapping("/chat")
@Validated
public class ChatController {
    @Resource
    private ApiService apiService;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private ChatConfigService chatConfigService;

    /**
     * 用户反馈
     */
    @ApiOperation(value = "用户反馈")
    @PostMapping("/errorBack")
    public Result<Boolean> errorBack() throws Exception {
        String key = "user_error_back" + DateTimeUtil.getCurrentDate();
        String user = String.valueOf(UserUtil.getUserId());
        if (redisUtil.sIsMember(key, user)) {
            return Result.success(true);
        }
        apiService.sendMail("1440651649@qq.com", "用户反馈信息", user + "用户点击了用户反馈, ip:" + WebUtil.getIp());
        redisUtil.sAdd(key, user);
        redisUtil.expire(key, 3600, TimeUnit.SECONDS);
        return Result.success(true);
    }

    /**
     * 用户通知
     */
    @ApiOperation(value = "用户通知")
    @GetMapping("/notice")
    public Result<String> notice() throws Exception {
        return Result.success(chatConfigService.findByKey(ChatConfigConstant.CHAT_NOTICE));
    }
}
